package com.banmoon.algorithm.order;

import java.util.Arrays;
import java.util.Random;

/**
 * 选择排序
 */
public class Demo05 {

    public static void main(String[] args) {
        int length = 10;
        int[] arr = new int[length];
        Random random = new Random();
        for (int i = 0; i < length; i++)
            arr[i] = random.nextInt(length);
        System.out.println("排序前的数组：" + Arrays.toString(arr));
        int[] sortArr = sort(arr);
        System.out.println("排序后的数组：" + Arrays.toString(sortArr));
    }

    public static int[] sort(int[] arr){
        for (int i = 0; i < arr.length-1; i++) {
            int tempIndex = i;
            for (int j = i+1; j < arr.length; j++) {
                if(arr[tempIndex]>arr[j])
                    tempIndex = j;
            }
            int temp = arr[i];
            arr[i] = arr[tempIndex];
            arr[tempIndex] = temp;
        }
        return arr;
    }

}
